package com.xy.servicemanage.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xy.servicemanage.db.PoolManager;

public class ServiceSetting {
	private static ServiceSetting m_instance = new ServiceSetting();
	private Map<String, Tdatabase> databaseMap = new HashMap<String, Tdatabase>();
	private Map<String, Tservices> serviceMap = new HashMap<String, Tservices>();
	private Map<Long, String> serviceDictMap = new HashMap<Long, String>();
	private Map<Long, List<Treqparams>> reqparamMap = new HashMap<Long, List<Treqparams>>();
	private Map<Long, List<Tresparams>> resparamMap = new HashMap<Long, List<Tresparams>>();
	private Map<String, Tservicecustomer> serviceCustMap = new HashMap<String, Tservicecustomer>();
	private long lastUpdate = System.currentTimeMillis();
	private long updateInteval = 15*60*1000;// 15*60*1000
	private Object lock = new Object();
	
	private void reload(){
			databaseMap.clear();
			serviceMap.clear();
			serviceDictMap.clear();
			reqparamMap.clear();
			resparamMap.clear();
			serviceCustMap.clear();
			init();
	}

	public static ServiceSetting getInstance() {
		return m_instance;
	}
	private   void checkUpdate(){
		if(	System.currentTimeMillis()-lastUpdate>updateInteval){
			lastUpdate=System.currentTimeMillis();
			reload();
		}
	}

	private ServiceSetting() {
		init();
/*		new Thread(){
			public void run() {
				Thread.sleep(60000);
			if(	System.currentTimeMillis()-lastUpdate>updateInteval){
				lastUpdate=System.currentTimeMillis();
				reload();
			}
			};
		}.start();*/
	}

	private void init() {
		try {
			Connection conn = PoolManager.getConnection("local");
			String sql = "SELECT isDynamicSql,sid, version, serviceId, serviceName,  execSql, SqlType, dataSourceID, isPage, returnTotal, returnSingle, returnList, returnCustomize ,isLog,logPattern FROM t_services ";
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				long sid = rs.getLong("sid");

				String isDynamicSql = rs.getString("isDynamicSql");
				String version = rs.getString("version");
				String serviceId = rs.getString("serviceId");
				String serviceName = rs.getString("serviceName");
				String execSql = rs.getString("execSql");
				String SqlType = rs.getString("SqlType");
				String dataSourceID = rs.getString("dataSourceID");
				String isPage = rs.getString("isPage");
				String returnTotal = rs.getString("returnTotal");
				String returnSingle = rs.getString("returnSingle");
				String returnList = rs.getString("returnList");
				String returnCustomize = rs.getString("returnCustomize");
				String isLog = rs.getString("isLog");
				String logPattern = rs.getString("logPattern");
				Tservices ts = new Tservices(isDynamicSql, sid, version,
						serviceId, serviceName, execSql, SqlType, dataSourceID,
						isPage, returnTotal, returnSingle, returnList,
						returnCustomize,isLog,logPattern);
				serviceMap.put(ts.getServiceId(), ts);
				serviceDictMap.put(Long.parseLong(ts.getServiceId()), serviceName);
			}
			rs.close();
			ps.close();

			sql = "SELECT custId, custName, serviceList, pubkey, prikey, bz FROM t_servicecustomer ";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				String custId = rs.getString("custId");
				String custName = rs.getString("custName");
				String serviceList = rs.getString("serviceList");
				String pubkey = rs.getString("pubkey");
				String prikey = rs.getString("prikey");
				String bz = rs.getString("bz");
				Tservicecustomer ts = new Tservicecustomer(custId, custName,
						serviceList, pubkey, prikey, bz);
				serviceCustMap.put(custId, ts);
			}
			rs.close();
			ps.close();

			sql = "SELECT dataSourceID, jdbcDriver, jdbcUrl, jdbcUser, JdbcPassword, bz FROM t_database";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				String dataSourceID = rs.getString("dataSourceID");
				String jdbcDriver = rs.getString("jdbcDriver");
				String jdbcUrl = rs.getString("jdbcUrl");
				String jdbcUser = rs.getString("jdbcUser");
				String JdbcPassword = rs.getString("JdbcPassword");
				String bz = rs.getString("bz");
				Tdatabase td = new Tdatabase(dataSourceID, jdbcDriver, jdbcUrl,
						jdbcUser, JdbcPassword, bz);
				databaseMap.put(dataSourceID, td);
			}
			rs.close();
			ps.close();

			sql = "SELECT isMustin,sid, reqid, reqname, reqtype, reqlen, reqformat,seq FROM t_reqparams  order by sid, seq";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				long sid = rs.getLong("sid");
				String reqid = rs.getString("reqid");
				String isMustin = rs.getString("isMustin");
				String reqname = rs.getString("reqname");
				String reqtype = rs.getString("reqtype");
				String reqlen = rs.getString("reqlen");
				String reqformat = rs.getString("reqformat");
				int sequence = rs.getInt("seq");
				Treqparams tq = new Treqparams(sid, reqid, reqname, reqtype,
						reqlen, reqformat, isMustin, sequence);
				List<Treqparams> l = reqparamMap.get(tq.getSid());
				if (l == null) {
					l = new ArrayList<Treqparams>();
					l.add(tq);
					reqparamMap.put(tq.getSid(), l);
				} else
					l.add(tq);
			}
			rs.close();
			ps.close();

			sql = "SELECT  sid,  resid,  resname,  restype,  reslen,  resformat,seq FROM t_resparams order by sid,seq";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				long sid = rs.getLong("sid");
				String resid = rs.getString("resid");
				String resname = rs.getString("resname");
				String restype = rs.getString("restype");
				String reslen = rs.getString("reslen");
				String resformat = rs.getString("resformat");
				int sequence = rs.getInt("seq");
				Tresparams tr = new Tresparams(sid, resid, resname, restype,
						reslen, resformat, sequence);
				List<Tresparams> l = resparamMap.get(tr.getSid());
				if (l == null) {
					l = new ArrayList<Tresparams>();
					l.add(tr);
					resparamMap.put(tr.getSid(), l);
				} else
					l.add(tr);

			}
			rs.close();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Tdatabase getDataBaseConfig(String databaseid) {
		synchronized (lock) {
			checkUpdate();	
		}
		return databaseMap.get(databaseid);
	}

	public Tservices getServiceByServiceId(String serviceId) {
		synchronized (lock) {
			checkUpdate();	
		}
		return serviceMap.get(serviceId);
	}

	public Tservicecustomer getServicecustomerByCustId(String custId) {
		synchronized (lock) {
			checkUpdate();	
		}
		return serviceCustMap.get(custId);
	}

	public List<Treqparams> getReqparamBySid(long serviceId) {
		synchronized (lock) {
			checkUpdate();	
		}
		return reqparamMap.get(serviceId);
	}
	public  Map<Long, String>  getServiceDictMap(){
	return serviceDictMap;
	}
	public List<Tresparams> getResparamBySid(long serviceId) {
		synchronized (lock) {
			checkUpdate();	
		}
		return resparamMap.get(serviceId);
	}

}
